<?php

/**
 * Created by PhpStorm.
 * User: yellow
 * Date: 2017/6/28
 * Time: 14:38
 */

namespace Admin\Service;

use Service\BaseService;

class JpushService extends BaseService {



    public function __construct() {
        parent::__construct();
        $this->pushModel = M('jpush_log');
    }

    public function jpushList($username,$reg_id,$content,$type,$isPage = 1, $nowPage = 1) {

        if ($username) {
            $where['username'] = array('like', '%'.$username.'%');
        }

        if ($reg_id) {
            $where['reg_id'] = array('like', '%'.$reg_id.'%');
        }


        if ($content) {
            $where['content'] = array('like', '%'.$content.'%');
        }

        if ($type) {
            $where['type'] = $type;
        }


        $obj = $this->pushModel->alias('ps')
            ->join('left join __QUESTION__ as q on ps.question_id = q.id')
            ->join('left join __ACCOUNTANT_USERS__ as au on ps.reg_id = au.registered_id')
            ->field('ps.*,au.username,q.title')
             ->order('ps.id desc');

        if ($where) {
            $obj->where($where);
        }
        if ($isPage) {
            $countRes = $obj->count();
            self::bPage($countRes);
            return self::resReturn(200, "success", self::$pageObj);
        } else {
            $lineList = $obj->page($nowPage, self::$pageNum)->select();
            return self::resReturn(200, 'success', $lineList);
        }
    }

    public function pushInfo($id) {
        $find = $this->pushModel
            ->where('id  = '.(int)$id)->find();

        return array(
            'info' => $find
        );
    }


    public function handle($data = array()){

        $act = $data['act'];
        switch ($act){

            case 'add':

                break;
            case 'edit':

                unset($data['act']);

                $res = $this->pushModel->save($data);

                break;
            case 'del':
                $where['id'] = $data['id'];
                $res = $this->pushModel->where($where)->delete();
                break;
        }

        if ($res){
            return self::resReturn(200, 'success');
        } else{
            return self::resReturn(130, '操作失败');
        }

    }



}
